<script setup lang="ts">
import { useLocale } from '~/modules/locale';

const props = defineProps<{
  gameCode: string;
}>();

const { cryptoName } = useCurrencyStore();
const router = useRouter();
const { locale } = useLocale();

(async () => {
  const data = await queryClient.fetchQuery({
    queryKey: [apis.apiGametLaunch.id],
    queryFn: () => apis.apiGametLaunch({ game_code: props.gameCode }),
    retry: 3,
  });
  router.back();

  if (!data?.game_token) {
    return;
  }

  const host = envs.isTestHost ? 'https://col.g9aaa.com' : data.domain;
  const url = new URL(`${host}/${props.gameCode}/index.html`);
  url.searchParams.set('token', data.game_token);
  url.searchParams.set('currency', cryptoName);
  url.searchParams.set('lang', locale.value);
  url.searchParams.set('return_url', location.origin);

  location.href = url.toString();
  Telegram.WebApp.BackButton.hide();
})();
</script>

<template>
  <div class="h-100 flex items-center justify-center">
    游戏启动中...
  </div>
</template>
